package com.ruoyi.web.controller.storage;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.Brand;
import com.ruoyi.purchase.service.BrandService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/repo/brand")
public class RepoBrandController extends BaseController {
    private String prefix = "repo/brand";
    @Autowired
    private BrandService brandService;

    @RequiresPermissions("repo:brand:view")
    @GetMapping()
    public String Brand()
    {
        return prefix + "/brand";
    }

    /**
     * 查询品牌信息列表
     */
    @RequiresPermissions("repo:brand:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Brand brand)
    {
        startPage();
        List<Brand> list = brandService.queryList(brand);
        return getDataTable(list);
    }

    /**
     * 导出品牌信息列表
     */
    @RequiresPermissions("repo:brand:export")
    @Log(title = "品牌信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Brand brand)
    {
        List<Brand> list = brandService.queryList(brand);
        ExcelUtil<Brand> util = new ExcelUtil<Brand>(Brand.class);
        return util.exportExcel(list, "brand");
    }

    /**
     * 品牌信息信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存品牌信息
     */
    @RequiresPermissions("repo:brand:add")
    @Log(title = "品牌信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Brand brand)
    {
        return toAjax(brandService.save(brand));
    }

    /**
     * 修改品牌信息
     */
    @GetMapping("/edit/{bid}")
    public String edit(@PathVariable("bid") Long brandId, ModelMap mmap)
    {
        Brand brand = brandService.getById(brandId);
        mmap.put("brand", brand);
        return prefix + "/edit";
    }

    /**
     * 修改保存品牌信息
     */
    @RequiresPermissions("repo:brand:edit")
    @Log(title = "品牌信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Brand brand)
    {
        return toAjax(brandService.updateById(brand));
    }

    /**
     * 删除品牌信息
     */
    @RequiresPermissions("repo:brand:remove")
    @Log(title = "品牌信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(brandService.removeByIds(Arrays.asList(ids)));
    }
}
